package com.example.jiaju.ui.main;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.example.jiaju.DataApplication;
import com.example.jiaju.R;
import com.example.jiaju.adapter.DecorateAdapter;
import com.example.jiaju.api.DecAndWorkApi;
import com.example.jiaju.databinding.ActivityDecorationBinding;
import com.example.jiaju.entity.DecAndWork;
import com.example.jiaju.entity.Decoration;
import com.example.jiaju.utils.MyItemDecoration;
import com.example.jiaju.utils.RetrofitUtils;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DecorationActivity extends AppCompatActivity {

    private String TAG="DecorationActivity";
    private ActivityDecorationBinding binding;
//    private DecorateAdapter adapter;
    public List<Decoration.PageDTO.ListDTO> list1;
    public List<DecAndWork.PageDTO> list;
    private DecorateAdapter adapter;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= DataBindingUtil.setContentView(this,R.layout.activity_decoration);

        //设置黑色状态栏字体以及重叠问题
        ImmersionBar.with(this).statusBarDarkFont(true).fitsSystemWindows(true).init();

        //查询所有的装修公司
        //todo 接口更新，直接级联查询一页装修公司以及作品
        /*DecorateApi decorateApi= RetrofitUtils.getRetrofit("http://81.70.202.116").create(DecorateApi.class);
        Call<Decoration> call=decorateApi.getAllDecoration(1,5);
        call.enqueue(new Callback<Decoration>() {
            @Override
            public void onResponse(Call<Decoration> call, Response<Decoration> response) {
                Decoration decoration=response.body();
                list1=decoration.page.list;
                //装修信息
                recyclerView=binding.decorateRecyclerview;
                adapter=new DecorateAdapter(list,DecorationActivity.this);
                recyclerView.setAdapter(adapter);
                recyclerView.addItemDecoration(new MyItemDecoration(8));
                recyclerView.setLayoutManager(new LinearLayoutManager(DecorationActivity.this,LinearLayoutManager.VERTICAL, false));
            }
            @Override
            public void onFailure(Call<Decoration> call, Throwable t) {

            }
        });*/
        DecAndWorkApi api=RetrofitUtils.getRetrofit(DataApplication.HTTP_URL).create(DecAndWorkApi.class);
        Call<DecAndWork> call=api.getAllDecAndWork(1,5);
        call.enqueue(new Callback<DecAndWork>() {
            @Override
            public void onResponse(Call<DecAndWork> call, Response<DecAndWork> response) {
                DecAndWork decAndWork=response.body();
                if(decAndWork==null){
                    Toast.makeText(DecorationActivity.this,"装修作品数据加载异常！",Toast.LENGTH_SHORT).show();
                }else{
                    list=decAndWork.page;
                    //装修信息
                    recyclerView=binding.decorateRecyclerview;
                    adapter=new DecorateAdapter(list,DecorationActivity.this);
                    recyclerView.setAdapter(adapter);
                    recyclerView.addItemDecoration(new MyItemDecoration(20));
                    recyclerView.setLayoutManager(new LinearLayoutManager(DecorationActivity.this,LinearLayoutManager.VERTICAL, false));
                }
            }

            @Override
            public void onFailure(Call<DecAndWork> call, Throwable t) {

            }
        });
        //回到HomeActivity
        binding.search1.returnBtn.setOnClickListener(v->finish());
    }
}